/**
 * 
 * @author Travis C lynch <travisclynch@student.midlandstech.edu>
 * @version Mar 22 2012
 * <p>
 * <b>Instructions:</b><br>
 * Write a program to input a text file, and, using a stack, process the entire file, looking for parentheses and curly braces, then to output whether the parentheses and curly braces are properly nested. 
 * Any characters other than parentheses and curly braces should be ignored. You can use TextFileReader.java, documented as in these JavaDoc pages, to input a text file from the same folder as the program file. 
 * You can use the Stack<E> class from java.util.
 * <p>
 * Note that a good text file to try your program out on is a .java file.  
 * Also note that .java files with parentheses or curly braces in comments or quotes may actually not be properly nested even if they compile. 
 * <p>
 * To insure proper Text input, be sure to add a blank line at the end of the input file.
 * Turn in well-documented Java source code, documented in the JavaDoc style so that the javadoc program would produce good output if it were run on it, to this dropbox.
 * <p>
 * <b>Description:</b><br>
 * A class that uses generic data types
 * 
 */
import java.io.*;
import java.util.*;

public class HW9 {
	private Stack<Character> stack; //Char data type stack variable imported from java.util. 
	/**
	 * @param fileData A BtringBuilder built string from a file to be parced and braces balanced 
	 */
	public void balance(String fileData) {
		stack = new Stack<Character>(); //Dim the stack
		for (int i = 0; i < fileData.length(); i++) {
			char chPair = fileData.charAt(i); //Char variable at i location within the file 
			switch (chPair) {
			case '(':
			case '<':
			case '{':
				stack.push(chPair); //pushes opening braces onto stack
				break;
			case ')':
			case '>':
			case '}':

				if (!stack.isEmpty()) { //checks to see if there is a matching opening brace
					char topStackCh = stack.pop();
					if ((chPair == '}' && !(topStackCh == '{'))
							|| (chPair == ')' && !(topStackCh == '('))
							|| (chPair == '>' && !(topStackCh == '<'))) {
						System.out.println("Unmatched " + chPair + " at " + i + 1);
						return;
					}
				} else {
					System.out.println("There is an extra " + chPair + " at character " + i + 1); //if stack is empty, then there is extra braces with out a pair
					return;
				}
				break;
			default: //default function for the switch
				break;
			}
		}

		if (!stack.isEmpty())System.out.println("There are no Brackets in this file."); //If the stack is empty after going trough the entire string, then there were no braces
		else System.out.println("All the Brackets are balanced"); //If there are braces in the stack and the logic above did not catch errors, then file is all good to go. 
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("C:/Users/Zendoren/Workspace/HW9/src/TextFileReader.java"));
		String input = null;
		StringBuilder sb = new StringBuilder();
		while ((input = br.readLine()) != null) sb.append(input);
		new HW9().balance(sb.toString());
	}

}
